package com.gaokuo.es.compose;

/**
 * 抽象节点
 * @author gaokuo
 */
public abstract class AbComposeNode {

    /**
     * 与前一个节点的组合方式
     */
    protected ComposeBindEnum bind;
    /**
     * 下一个节点
     */
    protected AbComposeNode next;
    /**
     * 上一个节点
     */
    protected AbComposeNode prev;


    /**
     * 添加node节点到末尾
     * @param node ~
     * @return this
     */
    public AbComposeNode addNode(AbComposeNode node) {
        addNodeTo(node,this);
        return this;
    }

    /**
     * 添加节点node到to末尾
     * @param node ~
     * @param to ~
     */
    protected void addNodeTo(AbComposeNode node, AbComposeNode to) {
        if(node.bind == null){
            throw new NullPointerException("非根节点的bin属性不可为空！");
        }
        AbComposeNode lastNode = to;
        while(lastNode.next != null){
            lastNode = lastNode.next;
        }
        lastNode.next = node;
        node.prev = lastNode;
    }

    public AbComposeNode(ComposeBindEnum bind) {
        this.bind = bind;
    }
}
